package com.southminority.ethnic.controller.admin.research;

import com.southminority.ethnic.common.result.PageResult;
import com.southminority.ethnic.common.result.R;
import com.southminority.ethnic.service.alioss.OssService;
import com.southminority.ethnic.service.research.IntellectualPropertyService;
import com.southminority.ethnic.controller.research.vo.IntellectualPropertyVO;
import com.southminority.ethnic.controller.alioss.vo.OssFileReqVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 知识产权管理端控制器
 * 
 * @author SouthMinority
 */
@Slf4j
@Tag(name = "知识产权管理后台", description = "管理员知识产权管理接口")
@RestController
@RequestMapping("/admin/research/intellectual-property")
@CrossOrigin
public class IntellectualPropertyAdminController {

    private final IntellectualPropertyService intellectualPropertyService;
    private final OssService ossService;

    public IntellectualPropertyAdminController(IntellectualPropertyService intellectualPropertyService,
                                             OssService ossService) {
        this.intellectualPropertyService = intellectualPropertyService;
        this.ossService = ossService;
    }

    /**
     * 分页查询知识产权列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询知识产权列表")
    public R<PageResult<IntellectualPropertyVO>> getIntellectualPropertyPage(
            @Parameter(description = "当前页") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "类型") @RequestParam(required = false) String type,
            @Parameter(description = "法律状态") @RequestParam(required = false) String legalStatus,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        try {
            PageResult<IntellectualPropertyVO> pageResult = intellectualPropertyService.getIntellectualPropertyPage(page, size, keyword, type, legalStatus, status);
            return R.ok(pageResult);
        } catch (Exception e) {
            log.error("分页查询知识产权失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有知识产权列表
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有知识产权列表")
    public R<List<IntellectualPropertyVO>> getAllIntellectualPropertyList() {
        try {
            List<IntellectualPropertyVO> intellectualPropertyList = intellectualPropertyService.getAllIntellectualPropertyList();
            return R.ok(intellectualPropertyList);
        } catch (Exception e) {
            log.error("获取所有知识产权列表失败", e);
            return R.fail("获取失败: " + e.getMessage());
        }
    }

    /**
     * 获取知识产权详情
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "获取知识产权详情")
    public R<IntellectualPropertyVO> getIntellectualPropertyDetail(@PathVariable Long id) {
        try {
            IntellectualPropertyVO intellectualPropertyVO = intellectualPropertyService.getIntellectualPropertyDetail(id);
            return R.ok(intellectualPropertyVO);
        } catch (Exception e) {
            log.error("获取知识产权详情失败", e);
            return R.fail("获取失败: " + e.getMessage());
        }
    }

    /**
     * 创建知识产权
     */
    @PostMapping("/create")
    @Operation(summary = "创建知识产权")
    public R<Boolean> createIntellectualProperty(@RequestBody IntellectualPropertyVO intellectualPropertyVO) {
        try {
            log.info("管理员创建知识产权: {}", intellectualPropertyVO.getTitle());
            boolean result = intellectualPropertyService.createIntellectualProperty(intellectualPropertyVO);
            return R.ok(result);
        } catch (Exception e) {
            log.error("创建知识产权失败", e);
            return R.fail("创建失败: " + e.getMessage());
        }
    }

    /**
     * 更新知识产权
     */
    @PutMapping("/update")
    @Operation(summary = "更新知识产权")
    public R<Boolean> updateIntellectualProperty(@RequestBody IntellectualPropertyVO intellectualPropertyVO) {
        try {
            log.info("管理员更新知识产权: ID={}, Title={}", intellectualPropertyVO.getId(), intellectualPropertyVO.getTitle());
            boolean result = intellectualPropertyService.updateIntellectualProperty(intellectualPropertyVO);
            return R.ok(result);
        } catch (Exception e) {
            log.error("更新知识产权失败", e);
            return R.fail("更新失败: " + e.getMessage());
        }
    }

    /**
     * 删除知识产权
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除知识产权")
    public R<Boolean> deleteIntellectualProperty(@PathVariable Long id) {
        try {
            log.info("管理员删除知识产权: ID={}", id);
            boolean result = intellectualPropertyService.deleteIntellectualProperty(id);
            return R.ok(result);
        } catch (Exception e) {
            log.error("删除知识产权失败", e);
            return R.fail("删除失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除知识产权
     */
    @DeleteMapping("/batch-delete")
    @Operation(summary = "批量删除知识产权")
    public R<Boolean> batchDeleteIntellectualProperty(@Parameter(description = "知识产权ID列表") @RequestBody List<Long> ids) {
        try {
            log.info("管理员批量删除知识产权: IDs={}", ids);
            boolean result = intellectualPropertyService.batchDeleteIntellectualProperty(ids);
            return R.ok(result);
        } catch (Exception e) {
            log.error("批量删除知识产权失败", e);
            return R.fail("批量删除失败: " + e.getMessage());
        }
    }

    /**
     * 更新知识产权状态
     */
    @PutMapping("/status/{id}")
    @Operation(summary = "更新知识产权状态")
    public R<Boolean> updateIntellectualPropertyStatus(
            @Parameter(description = "知识产权ID") @PathVariable Long id,
            @Parameter(description = "新状态") @RequestParam Integer status) {
        try {
            log.info("管理员更新知识产权状态: ID={}, Status={}", id, status);
            boolean result = intellectualPropertyService.updateIntellectualPropertyStatus(id, status);
            return R.ok(result);
        } catch (Exception e) {
            log.error("更新知识产权状态失败", e);
            return R.fail("更新状态失败: " + e.getMessage());
        }
    }

    /**
     * 设置知识产权重点展示
     */
    @PutMapping("/featured/{id}")
    @Operation(summary = "设置知识产权重点展示")
    public R<Boolean> setIntellectualPropertyFeatured(
            @Parameter(description = "知识产权ID") @PathVariable Long id,
            @Parameter(description = "是否重点展示") @RequestParam Integer isFeatured) {
        try {
            log.info("管理员设置知识产权重点展示: ID={}, IsFeatured={}", id, isFeatured);
            boolean result = intellectualPropertyService.setIntellectualPropertyFeatured(id, isFeatured);
            return R.ok(result);
        } catch (Exception e) {
            log.error("设置知识产权重点展示失败", e);
            return R.fail("设置失败: " + e.getMessage());
        }
    }

    /**
     * 上传知识产权证书图片
     */
    @PostMapping("/upload-certificate")
    @Operation(summary = "上传知识产权证书图片")
    public R<String> uploadCertificateImage(@Valid OssFileReqVo ossFileReqVo) {
        try {
            // 设置知识产权证书图片分类
            if (ossFileReqVo.getClassName() == null || ossFileReqVo.getClassName().isEmpty()) {
                ossFileReqVo.setClassName("intellectual-property-certificate");
            }
            
            log.info("管理员上传知识产权证书图片: 分类={}", ossFileReqVo.getClassName());
            String imageUrl = ossService.upload(ossFileReqVo.getFile(), ossFileReqVo.getClassName());
            return R.ok(imageUrl);
        } catch (Exception e) {
            log.error("上传知识产权证书图片失败", e);
            return R.fail("上传失败: " + e.getMessage());
        }
    }

    /**
     * 上传知识产权附件文件
     */
    @PostMapping("/upload-attachment")
    @Operation(summary = "上传知识产权附件文件")
    public R<String> uploadAttachment(@Valid OssFileReqVo ossFileReqVo) {
        try {
            // 设置知识产权附件分类
            if (ossFileReqVo.getClassName() == null || ossFileReqVo.getClassName().isEmpty()) {
                ossFileReqVo.setClassName("intellectual-property-attachment");
            }
            
            log.info("管理员上传知识产权附件: 分类={}", ossFileReqVo.getClassName());
            String fileUrl = ossService.upload(ossFileReqVo.getFile(), ossFileReqVo.getClassName());
            return R.ok(fileUrl);
        } catch (Exception e) {
            log.error("上传知识产权附件失败", e);
            return R.fail("上传失败: " + e.getMessage());
        }
    }

    /**
     * 测试服务连通性
     */
    @GetMapping("/test")
    @Operation(summary = "测试接口")
    public R<String> test() {
        return R.ok("知识产权管理服务正常");
    }
} 
